/* eslint-disable */
import Map from 'ol/Map'
import View from 'ol/View'
import TileLayer from 'ol/layer/Tile'
import {
	Group as LayerGroup
} from 'ol/layer'
import XYZ from 'ol/source/XYZ'
import TileWMS from 'ol/source/TileWMS'
// import OSM from 'ol/source/OSM';
import {
	transform
} from 'ol/proj.js'
import {
	defaults as defaultControls
} from 'ol/control.js'
import Overlay from 'ol/Overlay.js'
// import {fromLonLat} from 'ol/proj.js';
import Feature from 'ol/Feature'
import Point from 'ol/geom/Point'
import {
	Icon,
	Style,
	Fill,
	Stroke,
	Text
} from 'ol/style.js'
import VectorLayer from 'ol/layer/Vector'
import VectorSource from 'ol/source/Vector'
import ImageLayer from 'ol/layer/Image.js'
import Static from 'ol/source/ImageStatic.js'
import GeoJSON from 'ol/format/GeoJSON'
import {
	TopoJSON
} from 'ol/format'
import layer from './layer'
import style from './style'
import utils from './utils'
import feature from './feature'
import polygon from './polygon.js'
import overlay from './overlay.js'
import draw from './draw.js'
import {
	config
} from './config'
import {
	Tile,
	Group,
	Vector as LayerVector
} from 'ol/layer'
import WKT from 'ol/format/WKT'
import {
	fromExtent
} from 'ol/geom/Polygon'
import {
	defaults
} from 'ol/control';
import {
	getTopLeft,
	getWidth
} from 'ol/extent';
import WMTSTileGrid from 'ol/tilegrid/WMTS';
import {
	get as getProjection
} from 'ol/proj';
import WMTS from 'ol/source/WMTS';
const tk = '82919e61a578c973b520053b8c280436'
const lmap = {
	map: null,
	provinceLayer: null,
	provinceDetailLayer: null,
	provinceDetailLayerWhite: null,
	areaDetailLayer: null,
	maskLayer: null, // 地图蒙层
	// 创建XYZ图层
	mapLayer: new Tile({
		source: new XYZ({ // 天地图注记
			url: `http://t{1-12}.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=` + tk
		}),
		zIndex: 2
	}),
	// 创建XYZ图层
	tdLayer: new Tile({
		source: new XYZ({ // 网格图
			url: 'http://t{1-12}.tianditu.gov.cn/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=' + tk
		}),
		zIndex: 1
	}),
    // 高德地图图层
    gdLayer: new Tile({
        source: new XYZ({
            url: 'http://webrd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=2&scale=1&style=8'
        }),
        zIndex: 1
    }),
	// wmts图层
	createWmtsMap: function() {
		const projection = getProjection('EPSG:3857');
		const projectionExtent = projection.getExtent();
		const size = getWidth(projectionExtent) / 256;
		const resolutions = new Array(19);
		const matrixIds = new Array(19);
		for (let z = 0; z < 19; ++z) {
			// generate resolutions and matrixIds arrays for this WMTS
			resolutions[z] = size / Math.pow(2, z);
			matrixIds[z] = z;
		}
		return new TileLayer({
			source: new WMTS({
				url: 'https://mrdata.usgs.gov/mapcache/wmts',
				layer: 'sgmc2',
				matrixSet: 'GoogleMapsCompatible',
				format: 'image/png',
				projection: projection,
				tileGrid: new WMTSTileGrid({
					origin: getTopLeft(projectionExtent),
					resolutions: resolutions,
					matrixIds: matrixIds,
				}),
				wrapX: true
			}),
			zIndex: 99
		})
	},
// 创建地图
createMap: function(id) {
		this.map = new Map({
			target: id,
			layers: [this.gdLayer],
			view: new View({
				projection: 'EPSG:3857', // 设置坐标系,距离型
				center: utils.transform([255.4745, 39.7077]), 
				// center: utils.transform([473.263832107357, 23.1226255393605]), 
				zoom: 4,
				maxZoom: 18,
				// enableRotation: false/*  */
			}),
			controls: defaults({ // 控制栏配置
				zoom: false, // 缩放
				rotate: false, // 重置角度
				attribution: false
			})
		})
	},
	getExtent: function(map) {
		return map.getView().calculateExtent(map.getSize())
	},
	get4326Transform(lonlat) {
		return transform(lonlat, 'EPSG:4326', 'EPSG:3857')
	},
	get3857Transform(lonlat) {
		return transform(lonlat, 'EPSG:3857', 'EPSG:4326')
	},
	

	setLayerVector(index) {
		var layer = new VectorLayer({
			zIndex: index
		})
		return layer
	},
	// 静态图片
	setImageLayer(url, imageExtent) {
		return new ImageLayer({
			source: new Static({
				url: url,
				imageExtent: imageExtent,
				projection: 'EPSG:4326'
			})
		})
	}
}

lmap.layer = layer
lmap.style = style
lmap.utils = utils
lmap.feature = feature
lmap.config = config
lmap.polygon = polygon
lmap.overlay = overlay
lmap.draw = draw

export default lmap
